package com.situ.mall.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.situ.mall.common.entity.Member;
import com.situ.mall.common.exception.MyException;
import com.situ.mall.common.mapper.MemberMapper;
import com.situ.mall.common.service.IMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.situ.mall.common.util.RequestUtil;
import com.situ.mall.common.vo.ResultVO;
import com.situ.mall.common.vo.TableVO;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lwj
 * @since 2023-09-21
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements IMemberService {

    @Override
    public Member login(String username, String password) {
        Member m = baseMapper.selectOne(new LambdaQueryWrapper<Member>().eq(Member::getUsername, username));
        return m;
//        boolean b = m != null && m.getPassword().equals(password);//StringUtil.md5(password));
//        assert m != null;
//        if (b) {
//            RequestUtil.setCurrent(m);
//        }
//        return b;
    }

    @Override
    public TableVO select(Integer page, Integer limit, String username, String status) {
        PageHelper.startPage(page, limit);

        return TableVO.data(baseMapper.list(username, status));
    }

    @Override
    public void resetPassword(String ids) {
        baseMapper.resetPassword(ids.split(","));
    }

    @Override
    public void updateAll(String ids) {
        baseMapper.updateAll(ids.split(","));
    }

    @Override
    public void updateInformation(String username, String tel, String sex, String email) {
        Member m = (Member) RequestUtil.getCurrent();
        baseMapper.updateInformation(username, tel, sex, email, m.getId());
    }

    @Override
    public void updatePassword(String oldpassword, String newpassword1, String newpassword2) {
        Member m = (Member) RequestUtil.getCurrent();
        boolean b = checkPasswordFormat(newpassword1);
        if (!newpassword1.equals(newpassword2)) {
            throw new MyException("两次输入密码不一致");
        } else if (!oldpassword.equals(m.getPassword())) {
            throw new MyException("当前账号密码不正确！");
        } else if (!b) {
            throw new MyException("密码格式不正确！必须包含字母数字且6位以上");
        }
        baseMapper.updatePassword(newpassword1, m.getId());
    }

    @Override
    public Member selectByTel(String tel) {
        return baseMapper.selectByTel(tel);
    }

    @Override
    public void register(String tel, String password) {
        boolean b = checkPasswordFormat(password);
        if (!b) {
            throw new MyException("密码格式错误！必须包含字母数字且6位以上");
        }
        baseMapper.register(tel, password);

    }

    @Override
    public List<Member> getAvatar() {
        Member m = (Member) RequestUtil.getCurrent();
//        return baseMapper.selectAvatarById(m.getId());
        if (m == null) {
            return baseMapper.selectAvatarById(6);
        }
        List<Member> list = baseMapper.selectAvatarById(m.getId());
        return list;

    }

    private boolean checkPasswordFormat(String password) {
        String pattern = "^(?=.*[0-9])(?=.*[a-zA-Z])[0-9A-Za-z]{6,}$";
        return Pattern.matches(pattern, password);

    }


}
